/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.asu.cse360.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;
import org.joda.time.DateTime;

@Entity
@javax.persistence.Table(name="ordr")
public class Order implements Serializable{
    
    @Id
    @Column(name="order_id")
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Integer id;

    @ManyToOne
    @JoinColumn(name="table_id")
    private Table table;

    @Column(name="created_time")
    @Temporal(TemporalType.TIMESTAMP)
    private Calendar createdTime;

    @OneToMany
    @JoinTable(
	        name="order_item_order",
	        joinColumns=@JoinColumn(name="order_id"),
	        inverseJoinColumns=@JoinColumn(name="item_order_id"))
    private List<ItemOrder> itemOrders;

    @Transient
    private CustomerSession session;

    public Order()
    {
        this(null,null);
    }
    public Order(List<ItemOrder> itemOrders, Table table)
    {
        this.itemOrders = itemOrders;
        this.createdTime = new GregorianCalendar();
        this.table = table;
    }
    /**
     * @return the table
     */
    public Table getTable() {
        return table;
    }

    /**
     * @param table the table to set
     */
    public void setTable(Table table) {
        this.table = table;
    }

    /**
     * @return the createdTime
     */
    public DateTime getCreatedTime() {
        if(this.createdTime == null){
            return null;
        }
        return new DateTime(createdTime);
    }

    /**
     * @param createdTime the createdTime to set
     */
    public void setCreatedTime(DateTime createdTime) {
        if(createdTime == null){
            this.createdTime = null;
        }else{
            this.createdTime = createdTime.toCalendar(Locale.US);
        }
    }

    /**
     * @return the itemOrders
     */
    public List<ItemOrder> getItemOrders() {
        return itemOrders;
    }

    /**
     * @param itemOrders the itemOrders to set
     */
    public void setItemOrders(List<ItemOrder> itemOrders) {
        this.itemOrders = itemOrders;
    }

    public void addItemOrder(ItemOrder itemOrder){
        if(this.itemOrders == null){
            this.itemOrders = new ArrayList<ItemOrder>();
        }
        this.itemOrders.add(itemOrder);
    }

    public void removeItemOrder(ItemOrder itemOrder){
        if(this.itemOrders != null){
            for(Iterator<ItemOrder> iter = this.itemOrders.iterator();iter.hasNext();){
                ItemOrder temp = iter.next();
                if(temp.getId() != null && temp.getId().equals(itemOrder.getId())){
                    iter.remove();
                    break;
                }
            }
        }
    }

    /**
     * @return the id
     */
    public Integer getId() {
        return id;
    }

    /**
     * @param id the id to set
     */
    public void setId(Integer id) {
        this.id = id;
    }

    /**
     * @return the session
     */
    public CustomerSession getSession() {
        return session;
    }

    /**
     * @param session the session to set
     */
    public void setSession(CustomerSession session) {
        this.session = session;
    }

}
